<?php
/**
 * 后台_APP管理_控制器
 * @property App_version_mdl $version
 */
class APP extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('App_version_mdl','version');
	}

	public function upload_android(){
		
		$lastVersion = $this->version->getLastVersion('android');
		
		if( empty($_POST) ){
			$temp['info']  = $lastVersion;
			$temp['os'] = 'android';
			$this->load->view('admin/app/upload',$temp);
			return ;
		}

		if(empty($_FILES)){
			exit('未提交文件');
		}
		$input = $this->input->post(NULL,TRUE);
		if( empty($input['internal_version']) || !is_numeric($input['internal_version']) ){
			alert('内部版本号格式不正确',ADMIN_DIR.'app/upload_android');
			return ;
		}
		$dir     = 'download/';
		$downUrl = $dir.'runjoy_v'.$input['version'].'.apk';
		$filepath = FCPATH.$downUrl;
		if(file_exists($filepath)){
			alert('该版本('.$input['version'].')已存在',ADMIN_DIR.'app/upload_android');
			return ;
		}
		if( !is_dir( $dir ) ){
			mkdir($dir);
		}
		$res = move_uploaded_file($_FILES['app']['tmp_name'],$filepath);
		if($res===FALSE){
			alert('上传失败',ADMIN_DIR.'app/upload_android');
			return ;
		}

		$filesize=filesize($filepath);
		$filesize = number_format($filesize/1024/1024,2);
		$upload_time=time();

		// $this->load->model('Version_mdl','version');
		$data = array(
			'version'=>$input['version'],
			'internal_version'=>$input['internal_version'],
			'info'=>$input['info'],
			'app_url'=>base_url($downUrl),
			'filesize'=>$filesize,
			'upload_time'=>$upload_time,
			'system'=>1,
		);

		$this->version->insert($data);
		echo '文件上传成功!<br>下载链接为:'.$data['app_url'];
	}
	
	public function upload_ios(){

		$lastVersion = $this->version->getLastVersion('ios');
		
		if( empty($_POST) ){
			$temp['info']  = $lastVersion;
			$temp['os'] = 'ios';
			$this->load->view('admin/app/upload',$temp);
			return ;
		}
		
		$version = $this->input->post_get('version',TRUE);
		$internal_version = (int)$this->input->post_get('internal_version',TRUE);
		$info = $this->input->post_get('info',TRUE);
		$link = $this->input->post_get('link',TRUE);
		if(  !empty($lastVersion['internal_version']) && $internal_version < $lastVersion['internal_version']  ){
			alert('内部版本号不正确','back');
			return ;
		}
		$data = array(
				'version'=>$version,
				'internal_version'=>$internal_version,
				'info'=>$info,
				'app_url'=>$link,
				'filesize'=>0,
				'upload_time'=>time(),
				'system'=>2,
		);
		
		$this->version->insert($data);
		alert('信息更新成功','back');
	}
	
	
	
	
	
	
	
	
	
	
}
